package com.hdu.dwh.pojos.SQLBuilder;

import java.util.LinkedHashSet;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@lombok.EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SQLInsertBuilder extends SQLBulder{

    private String tableName;
    private String databaseName;

    private LinkedHashSet<String> columns = new LinkedHashSet<>();
    private String selectStatement;

    public SQLInsertBuilder() {
    }

    public SQLInsertBuilder insertInto(String databaseName, String tableName) {
        this.databaseName = databaseName;
        this.tableName = tableName;
        return this;
    }

    public SQLInsertBuilder selectFrom(String selectStatement) {
        this.selectStatement = selectStatement;
        return this;
    }


    public String build() {
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ");
        if (databaseName != null && !databaseName.isEmpty()) {
            sb.append(databaseName).append(".");
        }
        sb.append(tableName);

        if (columns != null && !columns.isEmpty()) {
            sb.append(" (");
            sb.append(String.join(", ", columns));
            sb.append(")");
        }

        if (selectStatement != null && !selectStatement.isEmpty()) {
            sb.append(" ").append(selectStatement);
        }

        return sb.toString();
    }
}

